package com.ocom.company.utils.sm2;

import com.ocom.common.utils.MD5;
import com.ocom.common.utils.NumConvertUtil;
import org.bouncycastle.crypto.params.ECPrivateKeyParameters;
import org.bouncycastle.crypto.params.ECPublicKeyParameters;
import org.bouncycastle.util.encoders.Hex;

import java.nio.charset.Charset;

public class Sm2DemoTest {
	
	public static final Charset charset = Charset.forName("utf-8");

    private static final String input = "1234";



    public static void main(String[] args) throws Exception {
//    	 SM2KeyPair sm2KeyPair = SM2KeyHelper.generateKeyPair();
    	 SM2KeyPair sm2KeyPair = new SM2KeyPair(
                 NumConvertUtil.hexStringToByte("4BECFA44FE4C9819F784B7DF7216288A6DB33F947C10B3413BAF3A12330F99DB"),
                 NumConvertUtil.hexStringToByte("EAC5FC31FD67D30224FF8F8918B00936F5392E1BB6C3653C69272A7111AF5B70"),
                 NumConvertUtil.hexStringToByte("EBAB7A0EEC89A627CF21595EAC64295307CC28CF02F3B284C8707A0638F4D609")
         );

    	 ECPublicKeyParameters ecPublicKeyParameters = SM2KeyHelper.buildECPublicKeyParameters(sm2KeyPair);
         ECPrivateKeyParameters ecPrivateKeyParameters = SM2KeyHelper.buildECPrivateKeyParameters(sm2KeyPair.getPrivateKey());
         
         System.out.println("publicKeyX:"+ Hex.toHexString(sm2KeyPair.getPublicKeyX()));
         System.out.println("publicKeyY:"+ Hex.toHexString(sm2KeyPair.getPublicKeyY()));
         System.out.println("privateKey:"+ Hex.toHexString(sm2KeyPair.getPrivateKey()));         
         System.out.println("publicKey:" + Hex.toHexString(ecPublicKeyParameters.getQ().getEncoded(false)));
         
//         SM2Helper.exportPublicKey(ecPublicKeyParameters.getQ(), "/Users/weachy/Dev/Java/publicKey.pem");
//         SM2Helper.exportPrivateKey(ecPrivateKeyParameters.getD(), "/Users/weachy/Dev/Java/privateKey.pem");

         /**

         //C1C2C3 mode
         byte[] encryptRet123 = SM2Helper.encrypt(input.getBytes(charset), ecPublicKeyParameters, SM2Engine.Mode.C1C2C3);
         System.out.println("SM2 encrypt C1C2C3 mode result:"+Hex.toHexString(encryptRet123));
         byte[] decryptRet123 = SM2Helper.decrypt(encryptRet123, ecPrivateKeyParameters, SM2Engine.Mode.C1C2C3);
         System.out.println("======解密===="+ Hex.toHexString(decryptRet123));


        //C1C3C2 mode
         byte[] encryptRet132 = SM2Helper.encrypt(input.getBytes(charset), ecPublicKeyParameters, SM2Engine.Mode.C1C3C2);
         System.out.println("SM2 encrypt C1C3C2 mode result:"+Hex.toHexString(encryptRet132));
         byte[] decryptRet132 = SM2Helper.decrypt(encryptRet132, ecPrivateKeyParameters, SM2Engine.Mode.C1C3C2);
         System.out.println("======解密2===="+new String(decryptRet132, charset));

         //sign and verifySign with ID
         SM2SignResult signRet = SM2Helper.sign(input.getBytes(charset), ecPrivateKeyParameters, ID.getBytes(charset));
         System.out.println("signResultR_mergeRS:"+Hex.toHexString(signRet.mergeRS()));
         System.out.println("signResultR_withID:"+Hex.toHexString(signRet.getSignR()));
         System.out.println("signResultS_withID:"+Hex.toHexString(signRet.getSignS()));
         System.out.println("input==="+Hex.toHexString(input.getBytes(charset)));
         boolean verifySignRet = SM2Helper.verifySign(input.getBytes(charset), signRet, ecPublicKeyParameters, ID.getBytes(charset));
         System.out.println("======签名校验1===="+verifySignRet);
     
         signRet = SM2Helper.sign(input.getBytes(charset), ecPrivateKeyParameters, null);
         System.out.println("signResultR_mergeRS:"+Hex.toHexString(signRet.mergeRS()));
         System.out.println("signResultR_withoutID:"+Hex.toHexString(signRet.getSignR()));
         System.out.println("signResultS_withoutID:"+Hex.toHexString(signRet.getSignS()));
         verifySignRet = SM2Helper.verifySign(input.getBytes(charset), signRet, ecPublicKeyParameters, null);
         System.out.println("======签名校验2===="+verifySignRet);

          **/

		 String md5s = MD5.MD5Encode(input,"utf8");
		 System.out.println(md5s);

		 System.out.println(NumConvertUtil.toGBK(md5s));


         SM2SignResult signRet = SM2Helper.sign(md5s.getBytes(charset), ecPrivateKeyParameters, null);
         System.out.println("input==="+input);
         System.out.println("signResultR_mergeRS:"+Hex.toHexString(signRet.mergeRS()));
         System.out.println("signResultR_withID:"+Hex.toHexString(signRet.getSignR()));
         System.out.println("signResultS_withID:"+Hex.toHexString(signRet.getSignS()));
         boolean verifySignRet = SM2Helper.verifySign(md5s.getBytes(charset), signRet, ecPublicKeyParameters, null);
         System.out.println("======签名校验2===="+verifySignRet);

//		String secKey = "EBAB7A0EEC89A627CF21595EAC64295307CC28CF02F3B284C8707A0638F4D609" ;
//		String url = "http://120.78.153.235:20000/o2api/Api/MenJin/GetJavaQRCode?key="+secKey+"&data=" + input;
//
//		String tt = HttpRequest.sendGet(url);
//		System.out.println(tt);


//		byte[] encryptRet132 = SM2Helper.encrypt(md5s.getBytes(charset), ecPublicKeyParameters, SM2Engine.Mode.C1C3C2);
//		System.out.println("SM2 encrypt C1C3C2 mode result:"+NumConvertUtil.bytesToHexString(encryptRet132));
//		byte[] decryptRet132 = SM2Helper.decrypt(encryptRet132, ecPrivateKeyParameters, SM2Engine.Mode.C1C3C2);
//		System.out.println("======解密2===="+new String(decryptRet132, charset));

	}
 
}